package com.kingschan.jeasytag.container.impl;


import java.io.File;
import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

import com.kingschan.jeasytag.container.EasyTagContainer;
import com.kingschan.jeasytag.core.parse.handle.AbstractHandle;
import com.kingschan.jeasytag.sql.AbstractConnection;
import com.kingschan.jeasytag.sql.query.Query;
import com.kingschan.util.lang.PropertiesUtil;
/**
 * 全局容器类
 * @author kingschan
 *date:2013-6-7
 */
public class EasyTagContainerImpl implements EasyTagContainer,Serializable{

	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(EasyTagContainerImpl.class);
	private String key;
	private AbstractConnection conn;
	private AbstractHandle handle;
	private String spath;
	private Query mysql_query;
	private Query sqlserver_query;
	private Query oracle_query;
	private String contentpath;
	private EasyTagContainerImpl(){}
	private static EasyTagContainerImpl instance = null;
	public static synchronized EasyTagContainerImpl getInstance(){
		if (null==instance) {
			instance= new EasyTagContainerImpl();
			try {
				instance.init();
			} catch (Exception e) {				
				e.printStackTrace();
			}
		}
		return instance;
	}
	
	private void init() throws Exception{
		
		File f = new File(this.getClass().getResource("/").getPath().concat("jeasytag.properties"));
		log.info(String.format("Load jeasyTag properties file :%s", f.getAbsolutePath()));
		Properties  p =PropertiesUtil.getProperties(f);
		Map<String, String> pmap=PropertiesUtil.getPropterties(p);
		  Set<?> keyValue = pmap.keySet();
		  Iterator<?> it = keyValue.iterator();
		  while (it.hasNext()) {
			 String key = (String) it.next();
			 log.info(String.format("key: %s value: %s", key,pmap.get(key)));
			}
         
		
		this.key=p.getProperty("jeasytag.key");
		this.spath=p.getProperty("jeasytag.serialization.folderName");
		this.handle=(AbstractHandle) Class.forName(p.getProperty("jeasytag.handle")).newInstance();
		if (p.getProperty("jeasytag.connectionClass").length()>0) {
			this.conn= (AbstractConnection) Class.forName(p.getProperty("jeasytag.connectionClass")).newInstance();
		}
		if (p.getProperty("sqlserver.default.query").length()>0) {
			this.sqlserver_query= (Query) Class.forName(p.getProperty("sqlserver.default.query")).newInstance();
		}
		if (p.getProperty("mysql.default.query").length()>0) {
			this.mysql_query= (Query) Class.forName(p.getProperty("mysql.default.query")).newInstance();
		}
		if (p.getProperty("oracle.default.query").length()>0) {
			this.oracle_query= (Query) Class.forName(p.getProperty("oracle.default.query")).newInstance();
		}
	}


	@Override
	public String getApplictionKey() {

		return this.key;
	}



	@Override
	public AbstractHandle getHandle() {

		return this.handle;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}


	

	public String getSpath() {
		return spath;
	}

	public void setSpath(String spath) {
		this.spath = spath;
	}

	public void setHandle(AbstractHandle handle) {
		this.handle = handle;
	}

	@Override
	public String getSerializationPath() {		
		return this.spath;
	}


	@Override
	public Query getMysqlQuery() {

		return this.mysql_query;
	}
	@Override
	public Query getOracleQuery() {

		return this.oracle_query;
	}
	@Override
	public Query getSqlServerQuery() {

		return this.sqlserver_query;
	}
	public Query getMysql_query() {
		return mysql_query;
	}
	public void setMysql_query(Query mysqlQuery) {
		mysql_query = mysqlQuery;
	}
	public Query getSqlserver_query() {
		return sqlserver_query;
	}
	public void setSqlserver_query(Query sqlserverQuery) {
		sqlserver_query = sqlserverQuery;
	}
	public Query getOracle_query() {
		return oracle_query;
	}
	public void setOracle_query(Query oracleQuery) {
		oracle_query = oracleQuery;
	}
	public String getContentpath() {
		return contentpath;
	}
	public void setContentpath(String contentpath) {
		this.contentpath = contentpath;
	}
	@Override
	public String getContentPath() {		
		return this.contentpath;
	}
	@Override
	public AbstractConnection getDefaultConnection() {		
		return this.conn;
	}
	


}